<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>主管课程申请</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<script src="js/jquery-2.0.3.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="css/x-admin.css" media="all">
<script src="lib/layui/layui.js" charset="utf-8"></script>
<link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="js/x-layui.js" charset="utf-8">
</head>
<body>

	<c:if test="${pd==null}">
		<c:redirect url="ManagerAppServlet?op=showManagerAppByPage"></c:redirect>
	</c:if>

	<div class="x-nav">
		<a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			href="javascript:location.replace(location.href);" title="刷新"><i
			class="layui-icon" style="line-height: 30px">ဂ</i></a>
	</div>
	<div class="x-body">
		
		<xblock>
		<button class="layui-btn layui-btn-danger" onclick="delAll()">
			<i class="layui-icon">&#xe640;</i>批量删除
		</button>
		<button class="layui-btn"
			onclick="member_add('添加课程列表','AddManagerApp.jsp','800','550')">
			<i class="layui-icon">&#xe608;</i>添加
		</button>
		<form action="ManagerAppServlet?op=showManagerAppByPage" method="post" style="display : inline-block;">
			<div class="layui-input-inline">
				<input type="hidden" id="selectkey" name="selectkey" value="<%=request.getAttribute("select")%>">
				<select class="layui-select" name="select" id="select"></select>
			</div>
			<div class="layui-input-inline">
				<input type="text" name="key" placeholder="请输入要查询的信息"
					id="deptname" autocomplete="off" class="layui-input" id="key"
					value="${key}"> 
			</div>
			<div class="layui-input-inline" style="width: 50px">
				<button class="layui-btn" type="submit" lay-submit=""
					lay-filter="sreach">
					<i class="layui-icon">&#xe615;</i>
				</button>
			</div>
		</form>
		<span class="x-right" style="line-height: 40px">共有数据：<span
			class="layui-badge">${pd.totalCount}</span> 条
		</span></xblock>
		<table class="layui-table">
			<thead>
				<tr>
					<th><input type="checkbox" name="ManagerApp" value="" id="all" class="one"></th>
					<th>申请课程编号</th>
					<th>部门编号</th>
					<th>部门名称</th>
					<th>课程名</th>
					<th>申请理由</th>
					<th>申请时间</th>
					<th>申请通过时间</th>
					<th>申请状态</th>
					<th>操作</th>
				</tr>
			</thead>

			<!-- 判断是否为空 -->
			<c:if test="${pd!=null}">
				<!-- 遍历 -->
				<c:forEach items="${pd.data}" var="ManagerApp">
					<tbody id="x-img">
						<tr>
							<td><input type="checkbox" class = "one" value="${ManagerApp.managerappNo}" name="ManagerApp"></td>
							<td>${ManagerApp.managerappNo}</td>
							<td>${ManagerApp.deptNo}</td>
							<td>${ManagerApp.ndn}</td>
							<td>${ManagerApp.deptName}</td>
							<td>${ManagerApp.applyReason}</td>
							<td>${ManagerApp.applyTime}</td>
							<td>${ManagerApp.passTime}</td>
							<td class="td-status"><c:if
									test="${ManagerApp.applyState==1}">
									<span class="layui-btn layui-btn-normal ">申请已通过</span>
								</c:if> <c:if test="${ManagerApp.applyState==0}">
									<span class="layui-btn layui-btn-danger ">正在申请中</span>
								</c:if>
								<c:if test="${ManagerApp.applyState==2}">
									<span class="layui-btn layui-btn-warm ">正在未通过</span>
								</c:if>
							</td>
							<td class="td-manage"><c:if
									test="${ManagerApp.applyState==1}">
									<a style="text-decoration: none"
										onclick="member_stop(this,'10001')" href="javascript:;"
										title="不通过申请"> <i class="layui-icon">&#xe601;</i>
									</a>
								</c:if> <c:if test="${ManagerApp.applyState==0}">
									<a style="text-decoration: none"
										onclick="member_start(this,'10001')" href="javascript:;"
										title="通过申请"> <i class="layui-icon">&#xe601;</i>
									</a>
								</c:if> </a> <a title="编辑" href="javascript:;"
								onclick="member_edit('编辑','UpdataManagerApp.jsp?managerappNo=${ManagerApp.managerappNo}&deptNo=${ManagerApp.deptNo}&deptName=${ManagerApp.deptName}&applyReason=${ManagerApp.applyReason}&ndn=${ManagerApp.ndn} ','4','','510')"
								class="ml-5" style="text-decoration: none"> <i
									class="layui-icon">&#xe642;</i>
							</a> <a title="删除" href="javascript:;" onclick="member_del(this,'1')"
								style="text-decoration: none" class="delmano"> <i
									class="layui-icon">&#xe640;</i>
							</a></td>
						</tr>
					</tbody>
				</c:forEach>
			</c:if>
		</table>

		<div id="page" style="text-align: center"></div>
	</div>
	<script src="layui/layui.all.js" charset="utf-8"></script>
	<script src="js/x-layui.js" charset="utf-8"></script>
	<script>
	layui.use(['laypage', 'layer'], function(){
		  var laypage = layui.laypage,
		  layer = layui.layer;
		  var key = 
		laypage.render({
			elem : 'page',
			theme:'#00BFFF',
			count : ${pd.totalCount},//数据总数，从服务端得到
			curr:${pd.page},//得到当前页，以便向服务端请求对应页的数据。
			
			layout : [ 'prev', 'page', 'next'],
			jump : function(obj,first) {
				var key = $("#key").val();
				var select = $("#select").val();
				 //obj包含了当前分页的所有参数，
				//首次不执行
			    if(!first){
			    	location.href = "ManagerAppServlet?op=showManagerAppByPage&page="
						+ obj.curr+"&key="+key+"&select="+select;
			    }
			}
		});

		});

		//批量删除提交
		$(function(){
		$("#all").click(function(){
		$("input[class = 'one' ]").prop("checked",this.checked);
		})
	});
	//批量删除提交
	function delAll() {
		var deleteAll = "";
		$(".one").each(function(){
			if($(this).is(':checked')){
				deleteAll += $(this).val()+","; 
			}else{}
		})
		
		layer.confirm('确认要删除吗？', {
			icon : 3,
			title : '提示信息'
		}, function(index) {
			//捉到所有被选中的，发异步进行删除
			$.ajax({
				type : "post",
				url : "ManagerAppServlet",
				data:{
					deleteAll : deleteAll,
					op : "delAll"
				},
				success:function(data){
					if(data == true){
						layer.msg('删除成功！', {
							icon : 1,
							time:1000
						});
						setTimeout(function(){
							window.location.reload();
							var index = parent.layer.getFrameIndex(window.name);
							parent.layer.close(index);
						},1000);
						return false;
					}else{
						layer.msg('删除失败！', {
							icon : 1,
							time:1000
						});
						return false;
					}
				},
				dataType : "json"
			});
			return false;
		});
	}
		
		
		/*用户-添加*/
		function member_add(title, url, w, h) {
			x_admin_show(title, url, w, h);
		}
		/*用户-查看*/
		function member_show(title, url, id, w, h) {
			x_admin_show(title, url, w, h);
		}

		/*用户-停用*/
		function member_stop(obj, id) {
			layer.confirm(
							'确认不通过申请吗？',
							function(index) {
								//发异步把用户状态进行更改
								$(obj)
											.parents("tr")
											.find(".td-manage")
											.prepend(
													'<a style="text-decoration:none" onClick="member_start(this,id)" href="javascript:;" title="通过申请"><i class="layui-icon">&#xe62f;</i></a>');
								var currentRow=$(obj).closest("tr");
								var managerappNo=currentRow.find("td:eq(1)").text();
								$.ajax({
									type : "post",
									url : "ManagerAppServlet",
									data : {managerappNo : managerappNo,op: "refuseManagerApp"},
									dataType : "json",
									success : function(data){
										if(data == true){
											location.replace(location.href);
											$(obj)
											.parents("tr")
											.find(".td-status")
											.html(
													//'<span class="layui-btn layui-btn-normal layui-btn-mini">申请未通过</span>');
								'<span class="layui-btn layui-btn-danger ">申请未通过</span>');
											
									$(obj).remove();
									
									layer.msg('申请未通过!', {
										icon : 5,
										time : 1000
									});
										}
										}
									});
								
							});
		}

		/*用户-启用*/
		function member_start(obj, id) {
			layer
					.confirm(
							'确认通过申请吗？',
							function(index) {
								//发异步把用户状态进行更改
								$(obj)
										.parents("tr")
										.find(".td-manage")
										.prepend(
												'<a style="text-decoration:none" onClick="member_stop(this,id)" href="javascript:;" title="不通过申请"><i class="layui-icon">&#xe601;</i></a>');
								
								var currentRow=$(obj).closest("tr");
								var managerappNo=currentRow.find("td:eq(1)").text();
								$.ajax({
									type : "post",
									url : "ManagerAppServlet",
									data : {managerappNo : managerappNo,op: "passManagerApp"},
									dataType : "json",
									success : function(data){
										if(data == true) {
											location.replace(location.href);
											$(obj)
											.parents("tr")
											.find(".td-status")
											.html(
													'<span class="layui-btn layui-btn-normal">申请已通过</span>');
									$(obj).remove();
									
									layer.msg('申请已通过!', {
										icon : 6,
										time : 1000
									});
										}
									}
								});
								
								
							});
		}
		// 用户-编辑
		function member_edit(title, url, id, w, h) {
			x_admin_show(title, url, w, h);
			
		}
		/*密码-修改*/
		function member_password(title, url, id, w, h) {
			x_admin_show(title, url, w, h);
			
		}
		
		/*用户-删除*/
		function member_del(obj, id) {
			
			layer.confirm('确认要删除吗？', {
				icon : 3,
				title : '提示信息'				
			}, function(index) {
				//发异步删除数据			
				var currentRow=$(obj).closest("tr");
			var managerappNo=currentRow.find("td:eq(1)").text();
			console.log("managerappNo:"+managerappNo);
				
				$.ajax({
					type : "post",
					url : "ManagerAppServlet",
					data : {managerappNo : managerappNo,op: "delManagerApp"},
					dataType : "json",
					success:function(data){
						if(data == true){
							layer.msg('已删除!', {
								icon : 1,
								time : 1000
							});
							$(obj).parents("tr").remove();
						}else
							layer.msg('未删除!', {
								icon : 1,
								time : 1000
							});
						console.log(data.flag);
					}
				});
				
				
			});
		}
	</script>
	<script type="text/javascript">
		var selectArr = ["课程名","部门号"];
		var selectKey = $("#selectkey").val();
		$(function(){
			if(selectKey != ""){
				$("#select").append("<option value = "+selectKey+">"+selectKey+"</option>");
				$.each(selectArr,function(i,o){
					if( o != selectKey){
						$("#select").append("<option value = "+o+">"+o+"</option>");
					}
				})
			}else{
				$.each(selectArr,function(i,o){
					$("#select").append("<option value = "+o+">"+o+"</option>");
				});
			}
		})
	</script>
</body>
</html>
